package com.sh.personnel.mapper;

import com.sh.personnel.domain.ITbRole;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

/**
 * @author 林思浩
 * @date 2020/02/27 1:59
 */
@Repository
public interface IRoleMapper {

    //根据用户ID获取角色集合
    @Select("SELECT role FROM tb_role t1, tb_user t2, tb_user_role t3 " +
            "WHERE t2.id = #{id} AND t2.id = t3.userid AND t1.id = t3.roleid")
    List<String> findRoleByUserId(String id);

    //根据用户ID获取该用户角色拥有的菜单ID
    @Select("SELECT menuid FROM tb_user t1, tb_user_role ur, tb_role_menu rm" +
            " WHERE t1.id = #{id} AND t1.id = ur.userid AND ur.roleid = rm.roleid")
    List<Integer> findRoleMenuIdByUserId(String id);

    //获取角色ID和名称
    @Select("SELECT id AS value, name AS label FROM tb_role")
    List<HashMap<String, String>> findRoleIDAndName();

    //根据查询条件获取角色列表
    List<ITbRole> findRoleList(String key);

    //添加角色
    @Insert("INSERT INTO tb_role (role, name, description, createtime)" +
            " VALUES (#{role}, #{name}, #{description}, #{createtime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertRole(ITbRole role);

    //根据ID获取角色信息
//    @Select("SELECT * FROM tb_role WHERE id = #{id}")
    ITbRole findRoleById(String id);

    //根据ID修改角色信息
    @Update("UPDATE tb_role SET" +
            " role = #{role}, name = #{name}, description = #{description}, createtime = #{createtime}" +
            "WHERE id = #{id}")
    int updateRoleById(ITbRole role);

    //根据ID删除角色
    @Delete("DELETE FROM tb_role WHERE id = #{id}")
    int deleteRoleById(String id);

    //根据角色ID删除其所有菜单
    @Delete("DELETE FROM tb_role_menu WHERE roleid = #{roleId}")
    int deleteRoleMenuByRoleId(Integer roleId);

    //根据角色ID批量添加菜单
    int insertRoleMenu(@Param("roleId") Integer roleId,
                       @Param("menuIds") List<Integer> menuIds);

}
